<?php 
namespace Wdl\model;
use Wdl\helper\webHelper;

class zufang extends base{

  /**
   * 获取在线快捷问答的预设问题
   * @return  
   */
  public function wenda()
  {
     $hid = isset( $this->_GPC['hid'])?  $this->_GPC['hid'] : '';
     $onsql = "";
     if(!empty($hid)){
        $house = pdo_fetch("select * from ".tablename("kbwdl_house")." where id='$hid'");
        if($house){
            $onsql = " or uid = '". $house['zhutype'] ."'" ;
        }
        
     }
     $items = pdo_fetchall("select * from ".tablename('kbwdl_replay').
        " where uniacid=:uniacid and (uid=99  $onsql ) and disabled=0 ",
        ['uniacid'=> $this->uniacid ]);
    $data = [];
    if($items){
        foreach($items as $item){
            $data[] = [
                'id'=> $item['id'],
                'title'=> $item['titlepic'],
                'content'=> $item['smalltext'],
            ];
        }
    }
    
    return $this->msg(0, 'success', $data);
  }
  /**
   * 搜索关键词
   * @return  
   */
  public function search()
  {
     $_GPC = $this->_GPC;
     $search = $_GPC['keyword']??'';
     if(empty($search)){
        return $this->msg(0, 'success', null);
     } 
     $type = webHelper::house_type();
     $data = null;
     foreach($type as $key=> $val){
        $condition = " where uniacid=:uniacid and isonline=1 and isdelete=0  and zhutype=:zhutype ";
        $condition .= " and (`title` like '%{$search}%' or `area` like '%{$search}%' or `village_name` like '%{$search}%')";
        $params = ['uniacid'=> $this->uniacid, 'zhutype'=> $val['zhutype']];

        $ret = webHelper::table_list_data('kbwdl_house', $condition, $params);
        if($ret['total']){
            $house = [];
            foreach($ret['list']  as $k=> $item){
                if($k > 5) break;
                $title = explode($search,$item['title']);
                $arr = [];
                if(count($title) ==1){
                    $arr[0] = ['text'=>$title[0],'color'=>''];
                    $arr[1] = ['text'=>$search,'color'=>'1'];
                }elseif(count($title) ==2 && empty($title[0])){
                    $arr[0] = ['text'=>$search,'color'=>'1'];
                    $arr[1] = ['text'=>$title[1],'color'=>''];
                }elseif(count($title) ==2 && !empty($title[0])){
                    $arr[0] = ['text'=>$title[0],'color'=>''];
                    $arr[1] = ['text'=>$search,'color'=>'1'];
                    $arr[2] = ['text'=>$title[1],'color'=>''];
                }else{
                    $arr[0] = $item['title'];
                }

                $house[$k] = [
                    'id'=> $item['id'],
                    'titles' => $arr,
                    'title'=> $item['title'],
                ];
            }
            $data[ $val['zhutype']] = [
                'subtitle' => '为您找到'.$val['name'].'信息'.$ret['total'].'条',
                'house' => $house,   
                'more' => $ret['total']>=5 ? 1: 0,   
                'zhutype'=> $val['zhutype'] ,     
            ];
        }
     }

     return $this->msg(0, 'success', $data);
  }
}